/**
 * 文件名：StockoutErpResourceService.java
 *
 * 创建人：Fyh - 1009173473@qq.com
 *
 * 创建时间：2019年9月9日 下午3:05:59
 *
 * 版权所有：南京植百汇智能科技服务有限公司
 */
package com.z100h.supplychain.evidence.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.z100h.supplychain.core.ErpServiceType;
import com.z100h.supplychain.core.EvidenceType;
import com.z100h.supplychain.evidence.entity.ErpResPrinter;
import com.z100h.supplychain.evidence.entity.ErpSku;
import com.z100h.supplychain.evidence.entity.StockoutErpResourceVo;
import com.z100h.supplychain.evidence.entity.StockoutSku;
import com.z100h.supplychain.evidence.mapper.StockoutErpResourceMapper;

/**
 * [描述信息：说明类的基本功能]
 *
 * @author Fyh - 1009173473@qq.com
 * @version 1.0 Created on 2019年9月9日 下午3:05:59
 */
@Service
public class StockoutErpResourceService {

	@Autowired
	private StockoutErpResourceMapper stockoutErpResourceMapper;

	public void insertOrderErpResource(String evidenceId, String userId, String orderId) {
		stockoutErpResourceMapper.insertOrderErpResource(evidenceId, userId, orderId, ErpServiceType.ORDERIN.getType());
	}

	public Integer getOrderErpResourceAmount(String orderId) {
		return stockoutErpResourceMapper.getOrderErpResourceAmount(orderId);
	}

	public List<String> getOrderErpResId(String orderId) {
		return stockoutErpResourceMapper.getOrderErpResId(orderId);
	}

	public void insertOrderErpResourceOfSale(String evidenceId, String userId, String orderId) {
		stockoutErpResourceMapper.insertOrderErpResourceOfSale(evidenceId, userId, orderId,
				ErpServiceType.ORDERIN.getType());
	}

	public void insertRepairErpResource(String evidenceId, String userId, String repairId) {
		stockoutErpResourceMapper.insertRepairErpResource(evidenceId, userId, repairId,
				ErpServiceType.REPAIRIN.getType());
	}

	public void productionCompletionByEvidenceId(String evidenceId) {
		stockoutErpResourceMapper.productionCompletionByEvidenceId(evidenceId);
	}

	public void productionCompletionByIds(List<String> ids) {
		stockoutErpResourceMapper.productionCompletionByIds(ids);
	}

	public List<StockoutErpResourceVo> findStockoutErpResourceVoListByEvidenceId(String evidenceId, String state) {
		return stockoutErpResourceMapper.findStockoutErpResourceVoListByEvidenceId(evidenceId, state);
	}

	public List<StockoutErpResourceVo> findQcErpResourceVoListByEvidenceId(String evidenceId) {
		return stockoutErpResourceMapper.findQcErpResourceVoListByEvidenceId(evidenceId);
	}

	public List<StockoutErpResourceVo> findOutErpResourceVoListByEvidenceId(String evidenceId) {
		return stockoutErpResourceMapper.findOutErpResourceVoListByEvidenceId(evidenceId);
	}

	public List<StockoutSku> findStockoutSkuErpResByIds(List<String> ids) {
		return stockoutErpResourceMapper.findStockoutSkuErpResByIds(ids);
	}

	public void qcFailedByEvidenceId(String evidenceId) {
		stockoutErpResourceMapper.qcFailedByEvidenceId(evidenceId);
	}

	public void qcFailedByIds(String evidenceId, List<String> ids) {
		stockoutErpResourceMapper.qcFailedByIds(evidenceId, ids);
	}

	/**
	 * 查ERP资源id对应的sku
	 * 
	 * @param erpResId
	 * @param serviceId
	 * @param evidenceType
	 * @return
	 * @author: Elvin Zhao - 279152260@qq.com 
	 * @date: Created on 2019年9月11日 下午5:42:55
	 */
	public List<ErpSku> findUnStockoutSkuListByErpResId(String evidenceId, String erpResId,
			List<String> orderServiceIds, List<String> repairServiceIds, EvidenceType evidenceType) {

		/**
		 * TODO 方法可能需要优化
		 */
		List<ErpSku> list = new ArrayList<ErpSku>();

		if (CollectionUtils.isNotEmpty(orderServiceIds)) {
			List<ErpSku> list1 = stockoutErpResourceMapper.findUnStockoutSkuListByErpOrderResId(evidenceId, erpResId,
					orderServiceIds);
			List<ErpSku> list2 = stockoutErpResourceMapper.findOtherUnStockoutSkuListByErpOrderResId(evidenceId,
					erpResId, orderServiceIds);

			if (CollectionUtils.isNotEmpty(list1)) {
				list.addAll(list1);
			}

			if (CollectionUtils.isNotEmpty(list2)) {
				list.addAll(list2);
			}

		}

		if (CollectionUtils.isNotEmpty(repairServiceIds)) {
			List<ErpSku> list3 = stockoutErpResourceMapper.findUnStockoutSkuListByErpRepairResId(evidenceId, erpResId,
					repairServiceIds);

			if (CollectionUtils.isNotEmpty(list3)) {
				list.addAll(list3);
			}

		}

		return list;

	}

	/**
	 * 
	 * 查order ERP资源id对应的sku
	 * 
	 * @param evidenceId
	 * @param erpResId
	 * @param orderServiceIds
	 * @param evidenceType
	 * @return
	 * @author: Elvin Zhao - 279152260@qq.com 
	 * @date: Created on 2019年10月11日 上午11:15:14
	 */
	public List<ErpSku> findUnStockoutSkuListByErpOrderResId(String evidenceId, String erpResId,
			List<String> orderServiceIds, EvidenceType evidenceType) {

		List<ErpSku> list = new ArrayList<ErpSku>();

		if (CollectionUtils.isNotEmpty(orderServiceIds)) {
			List<ErpSku> list1 = stockoutErpResourceMapper.findUnStockoutSkuListByErpOrderResId(evidenceId, erpResId,
					orderServiceIds);
			List<ErpSku> list2 = stockoutErpResourceMapper.findOtherUnStockoutSkuListByErpOrderResId(evidenceId,
					erpResId, orderServiceIds);

			if (CollectionUtils.isNotEmpty(list1)) {
				list.addAll(list1);
			}

			if (CollectionUtils.isNotEmpty(list2)) {
				list.addAll(list2);
			}

		}
		return list;
	}

	/**
	 * 
	 * 查repair ERP资源id对应的sku
	 * 
	 * @param evidenceId
	 * @param erpResId
	 * @param repairServiceIds
	 * @param evidenceType
	 * @return
	 * @author: Elvin Zhao - 279152260@qq.com 
	 * @date: Created on 2019年10月11日 上午11:15:36
	 */
	public List<ErpSku> findUnStockoutSkuListByErpRepairResId(String evidenceId, String erpResId,
			List<String> repairServiceIds, EvidenceType evidenceType) {

		List<ErpSku> list = new ArrayList<ErpSku>();

		if (CollectionUtils.isNotEmpty(repairServiceIds)) {
			List<ErpSku> list3 = stockoutErpResourceMapper.findUnStockoutSkuListByErpRepairResId(evidenceId, erpResId,
					repairServiceIds);

			if (CollectionUtils.isNotEmpty(list3)) {
				list.addAll(list3);
			}

		}
		return list;
	}

	/**
	 * 
	 * 通过单子查对应erp resid
	 * 
	 * @param evidenceId
	 * @return
	 * @author: Elvin Zhao - 279152260@qq.com 
	 * @date: Created on 2019年9月16日 下午2:59:40
	 */
	public List<String> findErpResIdByEid(String evidenceId) {
		return stockoutErpResourceMapper.findErpResIdByEid(evidenceId);
	}

	public List<String> findProductionCompletionErpResIdByEid(String evidenceId) {
		return stockoutErpResourceMapper.findProductionCompletionErpResIdByEid(evidenceId);
	}

	public List<String> findProductionCompletionErpOrderResIdByEid(String evidenceId) {
		return stockoutErpResourceMapper.findProductionCompletionErpOrderResIdByEid(evidenceId);
	}

	public List<String> findProductionCompletionErpRepairResIdByEid(String evidenceId) {
		return stockoutErpResourceMapper.findProductionCompletionErpRepairResIdByEid(evidenceId);
	}

	/**
	 * 
	 * 全量修改质检状态
	 * 
	 * @param evidenceId
	 * @param state
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年9月16日 下午3:18:30
	 */
	public void updateResQcState(String evidenceId, String state) {
		stockoutErpResourceMapper.updateResQcState(evidenceId, state);
	}

	/**
	 * 
	 * 批量修改质检状态
	 * 
	 * @param evidenceId
	 * @param erpResIds
	 * @param state
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年9月16日 下午3:18:45
	 */
	public void updateResQcStateByErpResIds(String evidenceId, List<String> erpResIds, String state) {
		stockoutErpResourceMapper.updateResQcStateByErpResIds(evidenceId, erpResIds, state);
	}

	/**
	 * 
	 * 全量修改出库状态
	 * 
	 * @param evidenceId
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年9月16日 下午3:30:36
	 */
	public void updateOutState(String evidenceId) {
		stockoutErpResourceMapper.updateOutState(evidenceId);
	}

	/**
	 * 
	 * 批量修改出库状态
	 * 
	 * @param ids
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年9月16日 下午3:30:48
	 */
	public void updateOutStateByIds(List<String> ids) {
		stockoutErpResourceMapper.updateOutStateByIds(ids);
	}

	/**
	 * 
	 * 查询没有出库的资源id
	 * 
	 * @param evidenceId
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年9月17日 上午11:09:37
	 */
	public List<String> findResNotOut(String evidenceId) {
		return stockoutErpResourceMapper.findResNotOut(evidenceId);
	}

	public List<String> findServiceId(String evidenceId, List<String> erpResIds) {
		return stockoutErpResourceMapper.findServiceId(evidenceId, erpResIds);
	}

	public List<ErpResPrinter> findErpResByEid(String evidenceId) {
		return stockoutErpResourceMapper.findErpResByEid(evidenceId);
	}

	public List<ErpResPrinter> findErpResByEids(List<String> evidenceIds) {
		return stockoutErpResourceMapper.findErpResByEids(evidenceIds);
	}

	public List<ErpResPrinter> findErpResByOrderIds(List<String> orderIds) {
		return stockoutErpResourceMapper.findErpResByOrderIds(orderIds);
	}

	public List<ErpSku> findNotAmountOutErpSkuList(String companyId) {
		return stockoutErpResourceMapper.findNotAmountOutErpSkuList(companyId);
	}

	public void updateStockoutErpSkuAmountIncrementOut(String evidenceId, Integer skuId, Integer amount) {
		stockoutErpResourceMapper.updateStockoutErpSkuAmountIncrementOut(evidenceId, skuId, amount);
	}

}
